package com.chookss.mine.set;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.StaticClass;
import com.chookss.base.Token;
import com.chookss.login.LoginActivity;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.johnrambo.ktea.ktExtends.ShareUtils;
import com.lvgroup.hospital.base.BaseAct;

import org.jetbrains.annotations.Nullable;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//注销
public class LogoutActivity extends BaseAct {

    @BindView(R.id.ivChoice)
    ImageView ivChoice;
    @BindView(R.id.common_title_txt)
    TextView common_title_txt;
    public boolean isSure = false;//默认没有同意协议

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logout);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initState();
        common_title_txt.setText("注销账号");
        ivChoice.setBackgroundResource(R.drawable.icon_no_choice_48);
    }

    public void unbindPUSHID() {
        //解绑设备id
        showLoading();
        Map<String, String> map = new HashMap<>();
        map.put("employeeCode", new ShareUtils().getString(LogoutActivity.this, StaticClass.EMPLOYEECODE, ""));
        map.put("companyCode", new ShareUtils().getString(LogoutActivity.this, StaticClass.COMPANYCODE, ""));
        MyHttpRequest.getDefault().getRequestCompat(null, Urls.unbindPUSHID, SignUtils2.makeSignStr(map), false, new ObserverCallback<String>() {
            @Override
            public void onSuccess(String data) {
                JSONObject reObject = null;
                try {
                    reObject = new JSONObject(data);
                    if ("200".equals(reObject.getString("code"))) {
                        Intent intent = new Intent();
                        intent.setAction(StaticClass.ACCOUNT_OUT);
                        LocalBroadcastManager.getInstance(LogoutActivity.this).sendBroadcast(intent);
                        quit();
                    } else {
                        MyToast.show(reObject.getString("msg"));
                        dismissLoading();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    dismissLoading();
                }
            }

            @Override
            public void onError(String errorMsg) {
            }
        });
    }

    private void quit() {
        MyHttpRequest.getDefault().postRequestCompat(null, Urls.logout, null, false,
                new ObserverCallback<String>() {
                    @Override
                    public void onSuccess(String data) throws Exception {
                        JSONObject reObject = new JSONObject(data);
                        dismissLoading();
                        if ("200".equals(reObject.getString("code"))) {
                            MyToast.show("注销成功");
                            Token.INSTANCE.quit();
                            new ShareUtils().putString(LogoutActivity.this, "token", "");
                            new ShareUtils().putString(LogoutActivity.this, StaticClass.USERNAME, "");
                            new ShareUtils().putString(LogoutActivity.this, StaticClass.ORIGINALPASS, "");
                            new ShareUtils().putString(LogoutActivity.this, StaticClass.EMPLOYEECODE, "");
                            new ShareUtils().putString(LogoutActivity.this, StaticClass.COMPANYNAME, "");
                            new ShareUtils().putString(LogoutActivity.this, StaticClass.COMPANYCODE, "");
                            Intent intent = new Intent(LogoutActivity.this, LoginActivity.class);
                            startActivity(intent);
                            finish();
                        } else {
                            MyToast.show(reObject.getString("msg"));

                        }
                    }

                    @Override
                    public void onError(String errorMsg) {
                        MyToast.show(Utils.onErrorTips(errorMsg));
                        dismissLoading();
                    }
                });
    }

    @OnClick({R.id.common_title_back, R.id.rlChoice, R.id.tvSure})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
            case R.id.rlChoice:
                if (isSure) {
                    isSure = false;
                    ivChoice.setBackgroundResource(R.drawable.icon_no_choice_48);
                } else {
                    isSure = true;
                    ivChoice.setBackgroundResource(R.drawable.icon_choice_48);
                }
                break;
            case R.id.tvSure:
                if (isSure) {
                    unbindPUSHID();
                } else {
                    MyToast.show("请阅读了解注销相关信息");
                }
                break;
        }
    }
}

